/*
 * @Description:  
 * @Version: 2.0
 * @Autor: ftt
 * @Date: 2022-06-11 14:22:40
 * @LastEditors: TT
 * @LastEditTime: 2022-06-22 22:30:53
 */

import 'package:hzy_normal_widget/hzy_normal_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:hzy_template/pages/my/routers/my_page_id.dart';
import '../config/config_index.dart';
import '../config/controller/user_state_controller.dart';
import '../pages/home/pages/home_drawer_page.dart';
import '../pages/login_module/routers/login_page_id.dart';

class CommonDrawerPage extends TTNormalStatefulWidget {
  const CommonDrawerPage({
    Key? key,
    this.drawerWidth = 250,
    this.onDrawerCall,
    this.screenView,
    this.animatedIconData = AnimatedIcons.arrow_menu,
    this.menuView,
    this.drawerIsOpen,
    this.screenIndex,
  }) : super(key: key);

  final double? drawerWidth;
  final Function(String)? onDrawerCall;
  final Widget? screenView;
  final Function(bool)? drawerIsOpen;
  final AnimatedIconData? animatedIconData;
  final Widget? menuView;
  final String? screenIndex;
  @override
  TTNormalState<TTNormalStatefulWidget> getState() {
    return _CommonDrawerPageState();
  }
}

class _CommonDrawerPageState extends TTNormalState<CommonDrawerPage>
    with TickerProviderStateMixin {
  /// 属性
  ///
  ///

  late ScrollController scrollController;
  late AnimationController iconAnimationController;
  late AnimationController animationController;

  @override
  bool get safeAreabottm => false;

  @override
  bool get safeAreatop => false;

  double scrolloffset = 0.0;

  /// 生命周期
  ///
  ///

  @override
  void initDefaultState() {
    animationController = AnimationController(
      vsync: this,
      duration: const Duration(
        milliseconds: 2000,
      ),
    );

    iconAnimationController = AnimationController(
      vsync: this,
      duration: const Duration(
        milliseconds: 0,
      ),
    );
    iconAnimationController.animateTo(
      1.0,
      duration: const Duration(milliseconds: 0),
      curve: Curves.fastOutSlowIn,
    );
    scrollController =
        ScrollController(initialScrollOffset: widget.drawerWidth!);
    scrollController.addListener(
      () {
        if (scrollController.offset <= 0) {
          if (scrolloffset != 1.0) {
            configsetState(
              () {
                scrolloffset = 1.0;
                if (widget.drawerIsOpen != null) {
                  widget.drawerIsOpen!(true);
                }
              },
            );
          }
          iconAnimationController.animateTo(
            0.0,
            duration: const Duration(milliseconds: 0),
            curve: Curves.fastOutSlowIn,
          );
        } else if (scrollController.offset > 0 &&
            scrollController.offset < widget.drawerWidth!) {
          iconAnimationController.animateTo(
            (scrollController.offset * 100 / (widget.drawerWidth!)) / 100,
            duration: const Duration(milliseconds: 0),
            curve: Curves.fastOutSlowIn,
          );
        } else if (scrollController.offset <= widget.drawerWidth!) {
          if (scrolloffset != 0.0) {
            setState(
              () {
                scrolloffset = 0.0;
                if (widget.drawerIsOpen != null) {
                  widget.drawerIsOpen!(false);
                }
              },
            );
          }
          iconAnimationController.animateTo(
            1.0,
            duration: const Duration(milliseconds: 0),
            curve: Curves.fastOutSlowIn,
          );
        }
      },
    );
    WidgetsBinding.instance.addPostFrameCallback((_) => getInitState());
    super.initDefaultState();
  }

  @override
  Widget createBody(BuildContext context) {
    return SingleChildScrollView(
      controller: scrollController,
      scrollDirection: Axis.horizontal,
      physics: const PageScrollPhysics(
        parent: ClampingScrollPhysics(),
      ),
      child: SizedBox(
        height: MediaQuery.of(context).size.height,
        width: MediaQuery.of(context).size.width + widget.drawerWidth!,
        child: Row(
          children: [
            SizedBox(
              width: widget.drawerWidth!,
              height: MediaQuery.of(context).size.height,
              child: AnimatedBuilder(
                animation: iconAnimationController,
                builder: (BuildContext context, Widget? child) {
                  return Transform(
                    transform: Matrix4.translationValues(
                        scrollController.offset, 0.0, 0.0),
                    child: HoemDrawerPage(
                      iconAnimationController: iconAnimationController,
                      callBackIndex: (p0) {
                        onDrawerClick(type: p0);
                      },
                    ),
                  );
                },
              ),
            ),
            SizedBox(
              width: MediaQuery.of(context).size.width,
              height: MediaQuery.of(context).size.height,
              child: Container(
                decoration: BoxDecoration(
                  color: HzyColorConfig().whitebackgroundColor,
                  boxShadow: <BoxShadow>[
                    BoxShadow(
                      color: CommentColorS.grey.withOpacity(0.6),
                      blurRadius: 24.r,
                    ),
                  ],
                ),
                child: Stack(
                  children: [
                    IgnorePointer(
                      ignoring: scrolloffset == 1 || false,
                      child: widget.screenView,
                    ),
                    if (scrolloffset == 1.0)
                      InkWell(
                        onTap: () {
                          configDraweAnimation();
                        },
                      ),
                    Positioned(
                      top: SizeMacro.statusBarHeight,
                      child: SizedBox(
                        width: AppBar().preferredSize.height,
                        height: AppBar().preferredSize.height - 3,
                        child: Material(
                          color: HzyColorConfig().whitebackgroundColor,
                          child: InkWell(
                            borderRadius: BorderRadius.circular(
                                AppBar().preferredSize.height),
                            child: Center(
                              child: widget.menuView ??
                                  AnimatedIcon(
                                    icon: widget.animatedIconData != null
                                        ? widget.animatedIconData!
                                        : AnimatedIcons.arrow_menu,
                                    progress: iconAnimationController,
                                  ),
                            ),
                            onTap: () {
                              configDraweAnimation();
                            },
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            )
          ],
        ),
      ),
    );
  }

  /// 网络请求
  ///
  ///

  /// 构建视图
  ///
  ///

  /// 触发方法
  ///
  ///

  Future<bool> getInitState() async {
    scrollController.jumpTo(
      widget.drawerWidth!,
    );
    return true;
  }

  void onDrawerClick({
    required int type,
  }) {
    configDraweAnimation();
    if (UserStateController.isLogin) {
      if (type == -2) {
        UserStateController.current.exitloginSuccess();
      } else if (type == 0) {
        currentToPage(MyPageId.mygeneral);
      }
    } else {
      currentToPage(LoginPageId.login);
    }
  }

  /// 点击关闭动画
  void configDraweAnimation() {
    if (scrollController.offset != 0.0) {
      scrollController.animateTo(
        0.0,
        duration: const Duration(milliseconds: 400),
        curve: Curves.fastOutSlowIn,
      );
    } else {
      scrollController.animateTo(
        widget.drawerWidth!,
        duration: const Duration(milliseconds: 400),
        curve: Curves.fastOutSlowIn,
      );
    }
  }
}
